package com.learn.image.service.impl;

import com.learn.image.enums.ReturnCodeAndMsgEnum;
import com.learn.image.service.ImageFileService;
import com.learn.image.utils.R;
import com.learn.image.utils.WaterMarkUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.UUID;

/**
 * 图片水印
 * @author: Administrator
 * @createTime: 2023/11/28 0028 18:14
 * @version: 1.0
 */
@Slf4j
@Service
public class ImageFileServiceImpl implements ImageFileService {

    @Override
    public R uploadImageFile(String text, MultipartFile imageFile) {
        // 文件保存路径
        String targetFilePath = "C:\\Users\\Administrator\\Desktop\\temp";
        // 重命名保存新文件的文件名
        String renameFileName = UUID.randomUUID().toString().replace("-", "");
        File targetFile = new File(targetFilePath + File.separator + renameFileName);
        try {
            String originalFilename = imageFile.getOriginalFilename();
            assert originalFilename != null;
            String typeName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            InputStream inputStream = imageFile.getInputStream();
//            OutputStream outputStream = new FileOutputStream(targetFile);
            OutputStream outputStream = Files.newOutputStream(targetFile.toPath());
            // 调用添加水印的方法
            WaterMarkUtil.imageMarkWater(text, inputStream, outputStream, -45, typeName);
            outputStream.close();
            inputStream.close();
            log.info("------图片上传、添加水印成功------");
        } catch (IOException e) {

        }
        return new R(ReturnCodeAndMsgEnum.SUCCESS, null);
    }

    @Override
    public String uploadImage(MultipartFile file, String uploadPath, String physicalUploadPath) {
        String filePath = physicalUploadPath + file.getOriginalFilename();
        log.info("------图片上传------filePath:{}", filePath);
        try {
            File targetFile = new File(filePath);
            FileUtils.writeByteArrayToFile(targetFile, file.getBytes());
        } catch (IOException e) {
            log.error("------图片上传------filePath:{}", filePath);
            e.printStackTrace();
        }
        return uploadPath + File.separator + file.getOriginalFilename();
    }

    @Override
    public R addWatermark(String waterContext, MultipartFile imageFile,String uploadPath,String dateTime) {
        try {
            // 文件保存路径
            String targetFilePath = "C:\\Users\\Administrator\\Desktop\\temp";
            // 保存新文件的文件名
            String originalFilename = imageFile.getOriginalFilename();
            assert originalFilename != null;
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

            File targetFile = new File(targetFilePath + File.separator + dateTime + extension);
            // 输出图片
            OutputStream outImgStream = Files.newOutputStream(targetFile.toPath());

            InputStream inputStream = imageFile.getInputStream();
            WaterMarkUtil.addWatermark(waterContext, inputStream, outImgStream, -45, extension);
            log.info("------图片上传、添加水印成功------extension:{}",extension);
        } catch (IOException e) {
            e.getMessage();
        }
        return new R(ReturnCodeAndMsgEnum.SUCCESS, null);
    }

}
